package com.sls.web.manager.action.recharge;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;

import com.google.gson.Gson;
import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.recharge.pojo.MatchSupplier;
import com.sls.web.manager.recharge.pojo.PhoneArea;
import com.sls.web.manager.recharge.pojo.Recharge;
import com.sls.web.manager.recharge.querybean.PhoneAreaQuery;
import com.sls.web.manager.recharge.service.PhoneAreaService;
import com.sls.web.manager.recharge.service.PhoneRechargeService;

public class PhoneRechargeAction extends SuperAction {
	private static final long serialVersionUID = 8374012917266562539L;

	@Resource(name = "phoneAreaService")
	private PhoneAreaService phoneAreaService;
	@Resource(name = "rechargeService")
	private PhoneRechargeService rechargeService;
	private String phone;
	private String idx;
	private PhoneArea phoneArea;
	private Recharge r;
	private List<MatchSupplier> rechargeList;

	public String phone() {
		return "phone";
	}

	public String moblie() {
		return "moblie";
	}

	public String findMsg() throws Exception {
		PhoneAreaQuery query = new PhoneAreaQuery();
		query.setPhone(phone);
		phoneArea = phoneAreaService.queryOne(query, getCurrentOperator());
		message=new Gson().toJson(phoneArea);
		return "ajax";
	}

	public String recharge() throws Exception {
		try {
			Boolean b = rechargeService.recharge(r, getCurrentOperator());
			if (!b) {
				r.setForceNation("true");
			} else {
				r = null;
			}
		} catch (ManagerException e) {
			r.setForceNation("false");
			this.addActionError(e.getMessage());
		}
		if (r != null && "01".equals(r.getPhoneType()))
			return this.moblie();
		else
			return this.phone();
	}
	
	public String viewRecharge() throws Exception {
		try{
			rechargeList=rechargeService.viewRecharge(r, getCurrentOperator());
			return "view";
		}catch (ManagerException e) {
			addActionError(e.getMessage());
			return "view";
		}
	}

	@JSON(serialize = true)
	public PhoneArea getPhoneArea() {

		return phoneArea;
	}

	public void setPhoneArea(PhoneArea phoneArea) {
		this.phoneArea = phoneArea;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@JSON(serialize = false)
	public String getPhone() {
		return phone;
	}

	public void setR(Recharge r) {
		this.r = r;
	}

	public Recharge getR() {
		return r;
	}

	public List<MatchSupplier> getRechargeList() {
		return rechargeList;
	}

	public void setRechargeList(List<MatchSupplier> rechargeList) {
		this.rechargeList = rechargeList;
	}

	public String getIdx() {
		return idx;
	}

	public void setIdx(String idx) {
		this.idx = idx;
	}
	
}
